/*
 *  txdnet.cn tonydon
 * 
 */
package txdnet.util.http;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;

/**
 *
 * @author txdnet
 */
public class HttpClientFactory {

    /**
     * 得到缺省设置的HttpClient 每路由为2, 最大连接为20, 无超时设置的HttpClient
     *
     * @return
     */
    public static HttpClient getDefaultHttpClient() {
        return getHttpClient(null);
    }

    /**
     * 得到一个特定设置的带有连接池的HttpClient
     *
     * @param setting
     * @return
     */
    public static HttpClient getHttpClient(HttpSetting setting) {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
        PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(schemeRegistry);
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        if (setting != null) {
            if (setting.getUseExpectContinue() != null) {
                params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, setting.getUseExpectContinue());
            }
            if (setting.getCharset() != null) {
                params.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, setting.getCharset());
                params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, setting.getCharset());
            }
            if (setting.getUserAgent() != null) {
                params.setParameter(CoreProtocolPNames.USER_AGENT, setting.getUserAgent());
            }
            if (setting.getSoTimeout() != null) {
                params.setParameter(CoreConnectionPNames.SO_TIMEOUT, setting.getSoTimeout());
            }
            if (setting.getConnectionTimeout() != null) {
                params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, setting.getConnectionTimeout());
            }
            if (Boolean.TRUE.equals(setting.getIgnoreCookies())) {
                params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
            }
            if (setting.getMaxTotalConnections() != null) {
                pccm.setMaxTotal(setting.getMaxTotalConnections());
            }
            if (setting.getMaxConnectionsPerRoute() != null) {
                pccm.setDefaultMaxPerRoute(setting.getMaxConnectionsPerRoute());
            }
        }
        return new DefaultHttpClient(pccm, params);
    }
}